import java.util.ArrayList;
import java.util.List;

public class Solution224 {
    public int calculate(String s) {
        s=s.replaceAll(" ","");
        s="(".concat(s).concat(")");
        List<Integer> list=new ArrayList<>();
        int count=0;
        while (count<s.length()){
            if (s.charAt(count)=='('){
                list.add(count);
            }else if (s.charAt(count)==')'){
                int p=list.remove(list.size()-1);
                int i=cal(s.substring(p+1,count));
                String t=String.valueOf(i);
                s=s.substring(0,p).concat(t).concat(s.substring(count+1));
                count=p+t.length()-1;
            }
            count++;
        }
        return Integer.parseInt(s);
    }

    int cal(String s){
        int count=0;
        int ans=0;
        int flag=1;
        while (count<s.length()){
            if (s.charAt(count)>='0' && s.charAt(count)<='9') {
                int current = 0;
                while (count < s.length() && s.charAt(count) >= '0' && s.charAt(count) <= '9') {
                    current = current * 10 + s.charAt(count) - '0';
                    count++;
                }
                ans=ans+flag*current;
                flag=1;
            }else{
                if (s.charAt(count)=='-'){
                    flag=-flag;
                }
                count++;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution224().calculate("(1+(4+5+2)-3)+(-6+8)"));
    }
}
